/**
 * 
 */
package com.ekingstar.framework.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 *
 */
public class ProxyFilter implements Filter {

	private Filter delegatingFilter = null;
	/* (non-Javadoc)
	 * @see javax.servlet.Filter#destroy()
	 */
	private List includeRegexPattern = null;
	private List excludeRegexPattern = null;
	
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		String includeUrl = filterConfig.getInitParameter("include-pattern");
		String excludeUrl = filterConfig.getInitParameter("exclude-pattern");
		if (includeUrl != null && !includeUrl.trim().equals("")){
			includeRegexPattern = new ArrayList();
			String[] urls =  includeUrl.split(",");
			for (int i = 0;i < urls.length;i++){
				if (urls[i] != null && !urls[i].trim().equals("")){
					includeRegexPattern.add(Pattern.compile(urls[i].trim()));
				}
			}
		}
		if (excludeUrl != null && !excludeUrl.trim().equals("")){
			excludeRegexPattern = new ArrayList();
			String[] urls =  excludeUrl.split(",");
			for (int i = 0;i < urls.length;i++){
				if (urls[i] != null && !urls[i].trim().equals("")){
					excludeRegexPattern.add(Pattern.compile(urls[i].trim()));
				}
			}
		}
		String targetClass = filterConfig.getInitParameter("targetClass");
		if (targetClass == null || targetClass.trim().equals("")){
			throw new ServletException("代理目标类没有填写");
		}
		try{
			Class targetFilterClass= Thread.currentThread().getContextClassLoader().loadClass(targetClass.trim());
			delegatingFilter = (Filter)targetFilterClass.newInstance();
			delegatingFilter.init(filterConfig);
		}catch(Throwable e){
			throw new ServletException(e.getMessage());
		}
	}
	
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest request = (HttpServletRequest)servletRequest;
		HttpServletResponse response = (HttpServletResponse)servletResponse;
		if(excludeRegexPattern != null){
			boolean match = isExcludeUrl(UrlUtils.getRequestUrl(request));
			if (match){
				// 这些  URL 不做过滤
				chain.doFilter(request, response);
				return;
			}
		}
		if (includeRegexPattern != null){
			boolean match = isIncludeUrl(UrlUtils.getRequestUrl(request));
			if (match){
				// 这些 URL 做过滤
				delegatingFilter.doFilter(request, response, chain);
				return;
			}
			else {
				//不做过滤
				chain.doFilter(request, response);
				return;
			}
		}
		//默认做过滤
		delegatingFilter.doFilter(request, response, chain);
	}

	private boolean isExcludeUrl(String url){
		if(excludeRegexPattern != null){
			Pattern pattern = null;
			for ( int i =0 ;i < excludeRegexPattern.size();i++){
				 pattern = (Pattern)excludeRegexPattern.get(i);
				 boolean match = pattern.matcher(url).matches();
				 if(match){
					 return true;
				 }
			}
		}
		return false;
	}
	private boolean isIncludeUrl(String url){
		if(includeRegexPattern != null){
			Pattern pattern = null;
			for ( int i =0 ;i < includeRegexPattern.size();i++){
				 pattern = (Pattern)includeRegexPattern.get(i);
				 boolean match = pattern.matcher(url).matches();
				 if(match){
					 return true;
				 }
			}
		}
		return false;
	}
	public void destroy() {
		// TODO Auto-generated method stub
		delegatingFilter.destroy();
	}

}
